home *** CD-ROM | disk | FTP | other *** search
- Path: maverick.tad.eds.com!news-admin@tad.eds.com
- From: fignet05.darrins@eds.com (Darrin Smith)
- Newsgroups: comp.lang.c
- Subject: To malloc (new) or not to malloc? Clarification. Please ignore previous post.
- Date: 9 Jan 1996 17:55:09 GMT
- Organization: CUSD
- Message-ID: <4cua5t$dm@maverick.tad.eds.com>
- References: <4ctvk3$ort@maverick.tad.eds.com>
- NNTP-Posting-Host: 148.94.42.90
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.11
-
- A few of you sent me email stating that fread returns an integer. You are of
- course correct. I didn't mean to say that fread returns a pointer, I was
- just using shorthand to tell you that I was using fread to get the record to
- store into the structure pointed to by sptr (that's why I wrote it as
- sptr=fread(...). Anyway, I should have been clearer. Here it is again with
- the shorthand taken out.
- _____________________________________________________________________________
-
- I know that I should know the answer to this (I'm sure one of my past
- instructors made it clear while I was sleep.... I mean sitting in class),
- but I can't remember so...
-
- Why is it that you can do something like the following:
-
- char *x;
-
- x="Some really long string with no particular meaning";
-
- and have no problems, but can't (safely) do something like this:
-
- struct st1{char one[10];
- char two[20];
- char three[10];
- };
-
- st1 *sptr; //or struct st1 *sptr in C instead of C++
-
- fread(sptr,sizeof(st1),1,infile);
-
- This caused a program I was trying to debug to crash. When I allocated
- memory for sptr (I used sptr=new st1; but I suppose malloc would have done
- just as well) it worked fine.
-
-
- What is going on here? Why isn't memory set aside for st1 *sptr just as
- it is for char *x? Before I did the new (or malloc) it seemed as though
- my program was getting written over!
-
- Why?
-
- Please respond here, or email me at: FIGNET05.darrins@eds.com
-
- Thanks.
-
-
-